package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import shuashuami.hb.com.adapter.ComplaintListAdatper;
import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpBMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.model.Complaint;
import shuashuami.hb.com.share.ShareCUserInfo;
import shuashuami.hb.com.util.LoadType;
import shuashuami.hb.com.view.pull.PullToRefreshLayout;

public class BComplaintListActivity extends AbActivity {
    private ListView listView;

    private int size = 10;
    private int maxpage = -1;
    private PullToRefreshLayout mptrl;
    private int page = 1;

    private ComplaintListAdatper adatper;
    private List<Complaint> mResources;

    public int operateType = LoadType.NO_OPERATE;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_complaint_list);

        listView = (ListView) findViewById(R.id.lv_listview);
        mptrl = (PullToRefreshLayout) findViewById(R.id.refresh_view);
    }

    @Override
    protected void initData() {
        setTitleBar("投诉记录");
        setLeftView();
        mResources = new ArrayList<>();
        adatper = new ComplaintListAdatper(mResources, this);
        listView.setAdapter(adatper);
        getMessage();
    }

    @Override
    protected void initListener() {
        setLeftViewListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mptrl.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {


            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                //
                operateType = LoadType.DO_REFERSH;
                page = 1;
                getMessage();

            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                if (maxpage == -1 || page == maxpage) {
                    mptrl.loadmoreFinish(PullToRefreshLayout.NODATA);
                    return;
                }
//
                operateType = LoadType.LOAD_MORE;
                page++;
                getMessage();
            }
        });
    }

    private void initAdapter(List<Complaint> list) {

        // 如果是刷新
        if (operateType == LoadType.DO_REFERSH) {
            mResources.clear();
        }

        mResources.addAll(list);

        adatper.notifyDataSetChanged();
        if (operateType == LoadType.DO_REFERSH) {
            mptrl.refreshFinish(PullToRefreshLayout.SUCCEED);
        }
        if (operateType == LoadType.LOAD_MORE) {
            mptrl.loadmoreFinish(PullToRefreshLayout.SUCCEED);
        }

        operateType = LoadType.NO_OPERATE;

    }

    private void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(BComplaintListActivity.this);
        HttpBMethods.getComplaintList(listener, shareCUserInfo.getId(),
                page + "", "10"
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        List<Complaint> list = new ArrayList<>();
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(BComplaintListActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            JSONObject t = jsonObject.getJSONObject("content");
                            page = t.getInt("currentpage");
                            maxpage = t.getInt("maxpage");
                            JSONArray jsonArray = t.getJSONArray("list");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                try {
                                    JSONObject temp = jsonArray.getJSONObject(i);
                                    Complaint complaint = new Complaint(temp.getString("shop_id"), temp.getString("status_text"), temp.getString("content"));
                                    list.add(complaint);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                            initAdapter(list);
                        } else {
                            //   ToastUtil.showShort(getActivity(), jsonObject.getString("message"));
                            initAdapter(list);
                        }

                    } catch (JSONException e) {
                        ToastUtil.showShort(BComplaintListActivity.this, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };
}
